<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-4-14
 * Time: 下午1:54
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report_model extends MY_Model {
    protected $table = 'report';
    public function get_list1($limit=5,$start=0,$order=false,$where=false,$fields="*",$table=FALSE,$join=false){
        if (!$table) {
            $table = $this->table;
        }

        $this->db
            ->select($fields)
            ->from($table);
        if($join && is_array($join)) {
            foreach($join as $row) {
                $this->db->join($row[0],$row[1],$row[2]);
            }

        }
        // =0 getall
        if ($limit) {
            $this->db->limit($limit,$start);
        }

        if ($order) {
            if (is_array($order)) {
                foreach ($order as $k => $v){
                    $this->db->order_by($k,$v);
                }
            }else if(is_string($order)){
                $this->db->order_by($order);
            }
        }else{
            if ($this->db->field_exists('sort_id',$table)) {
                $this->db->order_by('sort_id','desc');
            }else{
                $this->db->order_by('id','desc');
            }
        }
        if ($where) {
            if (is_string($where)) {
                $where = ' '.$where.' ';
            }elseif (is_array($where)) {
                $this->db->where($where);
            }
        }
        $query = $this->db->get();
        return $query->result_array();
    }
    public function get_count_all($where=FALSE,$table=FALSE,$join=false){
        if (!$table) {
            $table = $this->table;
        }

        $count = 0;
        if ($where === FALSE) {
            if($join && is_array($join)) {
                $count = $this->db->join($join[0],$join[1],$join[2])->count_all_results($table);
            } else {
                $count  = $this->db->count_all_results($table);
            }

        }else{
            if($join && is_array($join)) {

                $count  = $this->db->join($join[0],$join[1],$join[2])->where($where)->count_all_results($table);

            } else {
                //var_dump($this->db->last_query());die();
                $count  = $this->db->where($where)->count_all_results($table);
            }

        }
        return $count;
    }
    /**
     * 用户未进行审核的数量
     */
    public function get_status_nums(){
        $query = $this->db
            ->select('count(*) as total')
            ->from($this->table)
            ->where('status',0)
            ->get();
        return $query->row_array();
    }
    // 设定
    public function set($id,$arr)
    {
        $this->db->set($arr)
            -> where(array('id'=>$id))
            -> update($this->table);
        return $this->db->affected_rows();
    }
}